#include "sock.hpp"
#include "protocol.hpp"
#include <unistd.h>

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cout << "usage: " << argv[0] << " ip port" << std::endl;
        exit(1);
    }
    uint16_t port = atoi(argv[2]);
    std::string ip = argv[1];

    Sock sock;
    int sockId = sock.Socket();     // 创建套接字
    sock.Connect(sockId, port, ip); // 连接服务器

    while (true)
    {
        // 发送任务
        Request request;
        std::cout << "input:";
        std::cin >> request._x;
        std::cin >> request._op;
        std::cin >> request._y;

        std::string message = request.serialize(); // 序列化
        Send(sockId, message);                     // 发送给服务器
        std::string result;
        Recv(sockId, &result); // 获取结果
        Response response;
        response.unserialize(result); // 结果反序列化
        std::cout << "code: " << response._code << " result: " << response._result << std::endl;

        sleep(1);
    }

    return 0;
}